package com.edan.partnerback.common.optimization;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
import com.edan.partnerback.mapper.UserMapper;
import com.edan.partnerback.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Description 优化启动慢预热
 * @Author Edan
 * @Create 2024/3/29 15:02
 */
@Component
@Slf4j
public class initApplication implements ApplicationRunner {
    @Autowired
    UserMapper userMapper;

    /**
     * 项目启动后会运行这个方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            RedisCache.ping(); //redis探测
            userMapper.select1();//项目启动便加载一次数据库，防止懒加载
            log.info("数据库初始化");
            ThreadUtil.execAsync(() -> {//使用异步的方式进行项目启动优化
                HttpUtil.get("http://localhost:9090/");
                log.info("项目启动tomcat预热");
            });
        } catch (Exception e) {
            log.warn("启动优化失败");
        }
    }
}
